<template>
	<!-- 20230203 -->
	<view class="user-address-list-page">
		<!-- 页面内容 -->
		<view class="page-content padding-top-30">
			<view v-if="addressList.length">
				<view class="margin-bottom-20 padding-lr-30 bg-ff margin-lr-30 bg-shadow radius-40"
					v-for="(item,index) in addressList" :key="index">
					<!-- 地址 -->
					<view class="padding-tb-30 dashed-bottom-ed" @click="choose(index)">

						<!-- <view class="flex align-center">
							<view class="margin-right-20" v-if="item.is_default == 1">
								<ty-text-label :bgColor="'#1a1a1a'" text="默认" :radius="4" :padding="5"></ty-text-label>
							</view>
							<view class="line-height-30 text-cut-one text-28">
								{{item.province}}{{item.city}}{{item.area}}
							</view>
						</view> -->
						<view class="flex align-center justify-between">
							<text class="text-30">{{item.name}}</text>
							<text class="margin-left-30 text-30">{{item.phone}}</text>
						</view>
						<view class="margin-top-10 line-40 text-28 text-66">
							{{item.address}}
						</view>


					</view>

					<!-- 操作 -->
					<view class="height-100 flex align-center justify-between">
						<view @click="setDefault(index)" class="flex align-center">
							<text
								:class="item.is_default == 0 ? 'tyIcon-round text-30 text-cc' : 'tyIcon-round-check text-30 text-1a'"></text>
							<text class="margin-left-10 text-26"
								:class="item.is_default == 0 ? 'text-aa' : ''">{{item.is_default == 0 ? '设为默认' : '默认地址'}}</text>
						</view>

						<view class="flex">
							<view @click="go('pages/user/address/edit?id='+item.id)">
								<text class="tyIcon-edit text-30 text-aa"></text>
								<text class="margin-left-10 text-26 text-aa">编辑</text>
							</view>
							<view class="margin-left-60" @click="del(index)">
								<text class="tyIcon-delete text-30 text-aa"></text>
								<text class="margin-left-10 text-26 text-aa">删除</text>
							</view>
						</view>
					</view>
				</view>

				<!-- 底部条 -->


			</view>
			<view class="btn width-100p padding-lr-30" @click="go('pages/user/address/edit')">
				<view class="line-height-80 radius-40 bg-1a text-center text-28 text-ff">添加收货地址</view>
			</view>
		</view>

		<!-- 页面浮层 -->
		<view class="page-layer">

		</view>
	</view>
</template>
<script>
	import user from "../api/user.js"
	export default {
		data() {
			return {
				// 打开页面的方式 1--地址列表进入 2--选择地址进入
				type: 1,
				// 我的地址
				addressList: []
			}
		},

		onLoad(options) {
			this.type = options.type || 1
		},

		onShow() {
			this.loadList()
		},

		methods: {
			/**
			 * 设置默认
			 * @param {Object} index 索引
			 * @date 2023-02-03
			 */
			setDefault(index) {
				if (this.addressList[index].is_default == 0) {
					this.rq.getData('address/save_default', {
						id: this.addressList[index].id,
						is_default: 1
					}).then(res => {
						if (res.code == 1) {
							this.cn.toast(res.msg)
							this.loadList()
						}
					})
				} else if (this.addressList[index].is_default == 1) {
					this.rq.getData('address/save_default', {
						id: this.addressList[index].id,
						is_default: 0
					}).then(res => {
						if (res.code == 1) {
							this.cn.toast(res.msg)
							this.loadList()
						}
					})
				}

			},

			/**
			 * 删除地址
			 * @param {int} index 索引
			 * @date 2023-02-03
			 */
			del(index) {
				this.cn.confirm('是否确认删除该条地址').then(res => {
					if (res.confirm) {
						this.rq.getData('address/del_address', {
							id: this.addressList[index].id
						}).then(res => {
							if (res.code == 1) {
								this.cn.toast(res.msg)
								this.loadList()
							}
						})
					}
				})
			},

			/**
			 * 选择地址
			 * @param {Object} index
			 * @date 2023-02-03
			 */
			choose(index) {
				if (this.type == 2) {
					getApp().globalData.order.myAddressId = this.addressList[index].id
					uni.navigateBack({
						delta: 1
					})
				}
			},

			/**
			 * 加载列表
			 * @date 2023-02-03
			 */
			loadList() {
				this.rq.getData('address/address_list').then(res => {
					if (res.code == 1) {
						this.addressList = res.data
					}
				})
			}
		}
	}
</script>
<style scoped>
	.page-content {
		min-height: 100vh;
		background: #fff;
		padding-bottom: calc(env(safe-area-inset-bottom) + 140rpx);
	}

	.btn {
		position: fixed;
		left: 0;
		bottom: calc(30rpx + env(safe-area-inset-bottom));
	}
</style>